package com.itheima.controller;

/*
 * 验证码相关
 * */

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.JedisPool;

@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/send4order")
    public Result send4order(String telephone){
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        /*String validateCode = ValidateCodeUtils.generateValidateCode4String(4);*/
        //发送验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode.toString());
            //验证码发送成功,将验证码存入redis几分钟,用于用户登录校验
//            jedisPool.getResource().set(telephone+ RedisMessageConstant.SENDTYPE_ORDER,validateCode.toString());
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,5*60,validateCode.toString());
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }

    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        /*String validateCode = ValidateCodeUtils.generateValidateCode4String(4);*/
        //发送验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode.toString());
            //验证码发送成功,将验证码存入redis几分钟,用于用户登录校验
//            jedisPool.getResource().set(telephone+ RedisMessageConstant.SENDTYPE_ORDER,validateCode.toString());
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,5*60,validateCode.toString());
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }
}
